package com.fund.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fund.common.utils.UserContext;
import com.fund.user.config.JwtProperties;
import com.fund.user.mapper.UserMapper;
import com.fund.user.pojo.entity.User;
import com.fund.user.service.UserService;
import com.fund.user.utils.JwtTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private JwtTool jwtTool;

    @Autowired
    private JwtProperties jwtProperties;
    @Override
    public String login(User user) throws Exception {
        User tar = userMapper.selectOne(new LambdaQueryWrapper<User>()
                .eq(User::getUsername, user.getUsername())
                .eq(User::getPassword, user.getPassword()));
        if(tar == null){
            throw new Exception("用户名或密码错误");
        }
        UserContext.setUser(tar.getId());
        String token = jwtTool.createToken(tar.getId(), jwtProperties.getTokenTTL());
        jwtTool.parseToken(token);
        return token;
    }
}
